package com.sanno.maintain.controller;

import com.sanno.common.entity.SncUserLogin;
import com.sanno.common.result.Result;
import com.sanno.common.result.ResultCodeEnum;
import com.sanno.maintain.dao.MenuDao;
import com.sanno.maintain.entity.Childer1;
import com.sanno.maintain.entity.Childer2;
import com.sanno.maintain.entity.SccRoleMenu;
import com.sanno.maintain.entity.updateMenu;
import com.sanno.maintain.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/maintain/Menu")
@Api(tags = "系统维护-菜单Menu")
@CrossOrigin
public class Menu {

    @Resource
    private MenuDao menuDao;

    @Resource
    private MenuService menuService;

    @RequestMapping(value = "findMenu",method = RequestMethod.GET)
    @ApiOperation(value = "Menu",notes = "",httpMethod = "GET")
    public List<Childer1> findMenu(int menuId,int userId){
        List<Childer1> childer1List = menuDao.findMenu(menuId);
        for (Childer1 childer1 : childer1List) {
            List<Childer2> childer2List = menuService.queryMenu(menuId,childer1.getId(),userId);
            childer1.setChildren(childer2List);
        }
        return childer1List;
    }

    @RequestMapping(value = "updateMenu",method = RequestMethod.POST)
    @ApiOperation(value = "修改Menu",notes = "",httpMethod = "POST")
    public Result findMenu(int userId,int id,String menuIds){
        List<SncUserLogin> sncUserLogin = menuDao.findUser(userId,id);

        menuIds = menuIds + ",";
        String newIds = menuIds.substring(0,menuIds.lastIndexOf(","));
        String idArr[] = newIds.split(",");

        String updateId = "2,4,5,6,7,8,9,11,13,15,16,17,19,20,";
        String newupdateId = updateId.substring(0,updateId.lastIndexOf(","));
        String updateArr[] = newupdateId.split(",");
        List<updateMenu> menuList = new ArrayList<>();
        for (int i = 0; i < updateArr.length; i++) {
            updateMenu menu = new updateMenu();
            menu.setId(Integer.parseInt(updateArr[i]));
            menuList.add(menu);
        }
        if (sncUserLogin.size() > 0){
            menuDao.updateMenu3(menuList);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("Role_id",id);
        menuDao.deleteByMap(map);
        List<SccRoleMenu> list = new ArrayList<>();
        for (int i = 0; i < idArr.length; i++) {
            SccRoleMenu sccRoleMenu = new SccRoleMenu();
            sccRoleMenu.setRoleId(id);
            sccRoleMenu.setMenuId(Integer.parseInt(idArr[i]));
            list.add(sccRoleMenu);
            if (sncUserLogin.size() > 0){
                menuDao.updateMenu4(Integer.parseInt(idArr[i]));
            }
        }
        int rows = menuDao.addMenu(list);

        return Result.build(rows,ResultCodeEnum.SUCCESS);
    }

}
